package com.cxria.problem.security.handler

import com.cxria.problem.dto.Status
import com.cxria.problem.enums.StatusCodeEnum
import com.cxria.problem.enums.StatusMsgEnum
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.AuthenticationException
import org.springframework.security.web.authentication.AuthenticationFailureHandler

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class AuthFailureHandler implements AuthenticationFailureHandler{

    @Autowired
    ObjectMapper objectMapper

    @Override
    void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception){
        Status status = new Status(Status.ERROR, StatusCodeEnum.USER_INCORRECT_CREDENTIALS_ERROR, StatusMsgEnum.USER_INCORRECT_CREDENTIALS_ERROR)
        response.getWriter().append(objectMapper.writeValueAsString(status))
    }

}
